package cn.iocoder.yudao.module.common.service.knowledgepoint;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.common.controller.admin.knowledgepoint.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.knowledgepoint.KnowledgePointDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.common.dal.mysql.knowledgepoint.KnowledgePointMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.common.enums.ErrorCodeConstants.*;

/**
 * 知识点 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KnowledgePointServiceImpl implements KnowledgePointService {

    @Resource
    private KnowledgePointMapper knowledgePointMapper;

    @Override
    public Long createKnowledgePoint(KnowledgePointSaveReqVO createReqVO) {
        // 插入
        KnowledgePointDO knowledgePoint = BeanUtils.toBean(createReqVO, KnowledgePointDO.class);
        knowledgePointMapper.insert(knowledgePoint);
        // 返回
        return knowledgePoint.getId();
    }

    @Override
    public void updateKnowledgePoint(KnowledgePointSaveReqVO updateReqVO) {
        // 校验存在
        validateKnowledgePointExists(updateReqVO.getId());
        // 更新
        KnowledgePointDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgePointDO.class);
        knowledgePointMapper.updateById(updateObj);
    }

    @Override
    public void deleteKnowledgePoint(Long id) {
        // 校验存在
        validateKnowledgePointExists(id);
        // 删除
        knowledgePointMapper.deleteById(id);
    }

    private void validateKnowledgePointExists(Long id) {
        if (knowledgePointMapper.selectById(id) == null) {
            throw exception(KNOWLEDGE_POINT_NOT_EXISTS);
        }
    }

    @Override
    public KnowledgePointDO getKnowledgePoint(Long id) {
        return knowledgePointMapper.selectById(id);
    }

    @Override
    public PageResult<KnowledgePointDO> getKnowledgePointPage(KnowledgePointPageReqVO pageReqVO) {
        return knowledgePointMapper.selectPage(pageReqVO);
    }

}